package it.xiuxian.personcenter.fragment

import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import it.xiuxian.lib.base.ArouterAddress
import it.xiuxian.lib.base.BaseFragment
import it.xiuxian.lib.utils.*
import it.xiuxian.lib.utils.adapter.SlimAdapter
import it.xiuxian.lib.utils.adapter.SlimInjector
import it.xiuxian.lib.utils.adapter.injector.IViewInjector
import it.xiuxian.personcenter.R
import it.xiuxian.personcenter.bean.DetailListBean
import it.xiuxian.personcenter.bean.MyDetail
import it.xiuxian.personcenter.databinding.FragmentRecordBinding
import it.xiuxian.personcenter.presenter.RecordFragmentPresenter
import java.util.*

@Route(path = ArouterAddress.RECORDFRAGMENT)
class RecordFragment:BaseFragment<RecordFragmentPresenter>() {
    var binding:FragmentRecordBinding?=null
    var adapter1: SlimAdapter?=null
    @JvmField
    @Autowired
    var flag:String?=null
    override val layoutId: Int
        get() = R.layout.fragment_record
    override val presenter: RecordFragmentPresenter
        get() = RecordFragmentPresenter()

    override fun isEmpty(textView: TextView?): Boolean {
        return false
    }

    override fun initView() {
        ARouter.getInstance().inject(this)
        binding = getViewDataBinding<FragmentRecordBinding>()
        LiveDataBus.get().with(RxBusCode.RIZHIREFRESHCODE).observe(this, Observer {
            getRrefresh()
        })
        LiveDataBus.get().with(RxBusCode.RIZHILOADMORECODE).observe(this, Observer {
            if(next!=-1){
                getMore()
            }else{
                LiveDataBus.get().with(RxBusCode.RIZHILOADFINISHCODE).postValue(1981)
            }

        })
        binding!!.rv.layoutManager = LinearLayoutManager(requireActivity())
        adapter1 = SlimAdapter.create().register<MyDetail>(R.layout.personcenter_detail_item,
            object: SlimInjector<MyDetail> {
                override fun onInject(
                    data: MyDetail?,
                    injector: IViewInjector<out IViewInjector<*>>?
                ) {

                    injector!!.text(R.id.tv_tou,data!!.leixingname)
                        .text(R.id.tv_time,data.createdate)
                        .text(R.id.tv_num,data.money)

                }
            }).attachTo(binding!!.rv)

    }
    var i=1

    fun getMore(){
        if(flag=="1"){
            mPresenter!!.getDetail((++i).toString(),"1")
        }else if(flag=="2"){
            mPresenter!!.getDetail((++i).toString(),"2")
        }else{
            mPresenter!!.getDetail((++i).toString(),"3")
        }
    }
    fun getRrefresh(){
        i=1
        if(flag=="1"){
            mPresenter!!.getDetail("1","1")
        }else if(flag=="2"){
            mPresenter!!.getDetail("1","2")
        }else{
            mPresenter!!.getDetail("1","3")
        }
    }
    override fun initData(){
        if(flag=="1"){
            mPresenter!!.getDetail(i.toString(),"1")
        }else if(flag=="2"){
            mPresenter!!.getDetail(i.toString(),"2")
        }else{
            mPresenter!!.getDetail(i.toString(),"3")
        }

    }
    var newList = ArrayList<MyDetail>()
    var next=0
    fun setData(data: DetailListBean){
        if(data.list.isNotEmpty()){
            Log.e("newdata",data.toString())
            if(i==1){
                newList.clear()
                newList.addAll(data.list)
                adapter1!!.updateData(newList)
            }else if(i>1){
                newList.addAll(data.list)
                adapter1!!.updateData(newList)
            }
            next = data.next
        }
        LiveDataBus.get().with(RxBusCode.RIZHILOADFINISHCODE).postValue(1981)
    }


}